/**
 * 
 */
package com.gmail.hh.flickrj.sample.android.tasks;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import android.os.AsyncTask;

import com.gmail.hh.flickrj.sample.android.FlickrHelper;
import com.googlecode.flickrjandroid.Flickr;
import com.googlecode.flickrjandroid.contacts.Contact;
import com.googlecode.flickrjandroid.oauth.OAuth;
import com.googlecode.flickrjandroid.oauth.OAuthToken;
import com.googlecode.flickrjandroid.people.User;

public class LoadUserForContactsTask extends AsyncTask<OAuth, Void, User > {

	/**
	 *  Load Contacts asynchron, invoke callback handler after loading
	 *  author: helmut
	 */
	
	private String mSearchUsername;
	
	// each Class using this Loader  must implement this interface
	IUserLoadedListener mCallbackUserLoaded;
	public interface IUserLoadedListener {
		public void onUserLoaded(User user);
	}
	

	/**
	 * @param flickrjAndroidSampleActivity
	 */
	public LoadUserForContactsTask(IUserLoadedListener callbackUserLoaded, String searchUsername) {
		this.mCallbackUserLoaded = callbackUserLoaded;
		this.mSearchUsername = searchUsername;
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(Params[])
	 */
	@Override
	protected User doInBackground(OAuth... arg0) {
		OAuthToken token = arg0[0].getToken();
		Flickr f = FlickrHelper.getInstance().getFlickrAuthed(token.getOauthToken(), 
				token.getOauthTokenSecret());
		try {
			// Attention: the api findByUsername returns only the username and the id in a user-object
			User user = f.getPeopleInterface().findByUsername(mSearchUsername);
			// fetch the complete data for this user 
			if (user!= null && user.getId()!=null ) {
				User userInfo = f.getPeopleInterface().getInfo(user.getId());
				if (userInfo!=null ) {
					user = userInfo;
				}
			}
			return user;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(User user) {
		if (this.mCallbackUserLoaded != null ) {
			this.mCallbackUserLoaded.onUserLoaded(user);
		}
	}
	
}